using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FBGraph.Demos.AspNetMvc.Models.View.Photos;
using FBGraph.Web.Mvc;

namespace FBGraph.Demos.AspNetMvc.Controllers
{
    [FBGraphAuthorize(ExtendedPermission.UserPhotos)]
    public class PhotosController : BaseController
    {
        public ActionResult Index()
        {
            using (FbContext.StartAsync())
            {
                var albums = FbContext.Albums.GetFor("me");
                var photos = FbContext.Photos.GetFor("me");

                FbContext.CompleteAsync();

                var model = new IndexData
                {
                    Albums = albums,
                    Photos = photos
                };

                return View(model);
            }
        }

        public ActionResult Album(Int64 id)
        {
            using (FbContext.StartAsync())
            {
                var album = FbContext.Albums.Get(id);
                var photos = FbContext.Photos.GetFor(id);

                FbContext.CompleteAsync();

                var model = new AlbumData
                {
                    Album = album,
                    Photos = photos
                };

                return View(model);
            }
        }

        public ActionResult Photo(Int64 id)
        {
            var photo = FbContext.Photos.Get(id);

            return View(photo);
        }

        public ActionResult Upload(Int64 albumId)
        {
            var file = Request.Files[0];

            var photoId = FbContext.Photos.Upload(albumId, null, file.FileName, file.ContentType, file.InputStream);

            this.CreatedIds.Add(photoId.ToString());

            return RedirectToAction("album", new { id = albumId });
        }

        public ActionResult Create(String name)
        {
            var albumId = FbContext.Albums.Create(null, name, "Created by FBGraph.NET");

            this.CreatedIds.Add(albumId.ToString());

            return RedirectToAction("album", new { id = albumId });
        }
    }
}